package
{
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.utils.Dictionary;

    public class JSONfy
    {
        public function JSONfy()
        {

        }

        // just in case there's a need to write the basic tween settings
        public static function write_tween_defaults():void
        {
            var tween_sequences:Vector.<int> = new <int>[
                TweenTypeConstants.ZOOM,
                TweenTypeConstants.SCAN,
                TweenTypeConstants.FADETOZERO,
                TweenTypeConstants.CHANGEIMAGE,
                TweenTypeConstants.FADETOONE
            ];

            var tween_settings:Vector.<Number> = new <Number>[
                1.0, // zoom time
                1.1, // zoom scale factor

                5.0, // scan time
                0.0, // scan transition id

                1.0, // fade to zero time

                1.0, // fade to one time
            ];

            var lines:String = make_lines(tween_sequences, tween_settings);

            var tween_file:File = File.applicationStorageDirectory.resolvePath("tweens.txt");
            var tween_stream:FileStream = new FileStream();

            tween_stream.open(tween_file, FileMode.WRITE);
            tween_stream.writeUTFBytes(lines);
            tween_stream.close();
        }

        public static function load_tweens(game:Game):void
        {
            var tween_file:File = File.applicationStorageDirectory.resolvePath("tweens.txt");

            if (! tween_file.exists )
            {
                write_tween_defaults();
            }

            var file_stream:FileStream = new FileStream();
            file_stream.open(tween_file, FileMode.READ);

            var json_strings:String = file_stream.readUTFBytes(file_stream.bytesAvailable);
            var line:Array = json_strings.split("\n");

            var sequences:Vector.<int>   = new Vector.<int>();
            var settings:Vector.<Number> = new Vector.<Number>();

            for (var i:int = 0; i < line.length; ++i)
            {
                if (line[i] == "") continue;

                var object:Object = JSON.parse(line[i]);

                var id:int = TweenTypeConstants.int_map[object.name];

                sequences.push(id);

                if (object.settings != null)
                {
                    for (var j:int = 0; j < object.settings.length; ++j)
                    {
                        settings.push(object.settings[j]);
                    }
                }
            }

            file_stream.close();

            game.tween_sequences = sequences;
            game.tween_settings  = settings;
        }

        private static function make_lines(seq:Vector.<int>, sett:Vector.<Number>): String
        {
            var lines:String = new String();
            var idx:int = 0;

            for (var i:int = 0; i < seq.length; ++i)
            {
                var object:TweenObject  = new TweenObject();
                var s_id:int = seq[i];

                object.name = TweenTypeConstants.str_map[s_id];

                if (s_id == TweenTypeConstants.ZOOM)
                {
                    object.settings = new <Number>[sett[idx], sett[++idx]];
                    ++idx;
                }

                if (s_id == TweenTypeConstants.FADE)
                {

                }

                if (s_id == TweenTypeConstants.FADETOZERO)
                {
                    object.settings = new <Number>[sett[idx]];
                    ++idx;
                }

                if (s_id == TweenTypeConstants.FADETOONE)
                {
                    object.settings = new <Number>[sett[idx]];
                    ++idx;
                }

                if (s_id == TweenTypeConstants.SCAN)
                {
                    object.settings = new <Number>[sett[idx], sett[++idx]];
                    ++idx;
                }

                if (s_id == TweenTypeConstants.CHANGEIMAGE)
                {

                }

                lines += JSON.stringify(object) + "\n";
            }

            return lines;
        }

        public static function save_tweens(game:Game):void
        {
            var lines:String = make_lines(game.tween_sequences, game.tween_settings);

            var tween_file:File = File.applicationStorageDirectory.resolvePath("tweens.txt");
            var tween_stream:FileStream = new FileStream();

            tween_stream.open(tween_file, FileMode.WRITE);
            tween_stream.writeUTFBytes(lines);
            tween_stream.close();
        }

        public static function make_config():void
        {
            var lines:String = JSON.stringify( { img_dir: ".", test: "testing", show_stats: "true" } );
            var tween_file:File = File.applicationStorageDirectory.resolvePath("config.txt");
            var tween_stream:FileStream = new FileStream();

            tween_stream.open(tween_file, FileMode.WRITE);
            tween_stream.writeUTFBytes(lines);
            tween_stream.close();
        }

        public static function load_config():Dictionary
        {
            var config_file:File = File.applicationStorageDirectory.resolvePath("config.txt");

            if (! config_file.exists )
            {
                make_config();
            }

            var file_stream:FileStream = new FileStream();

            file_stream.open(config_file, FileMode.READ);
            var json_strings:String = file_stream.readUTFBytes(file_stream.bytesAvailable);
            file_stream.close();

            var config:Object = JSON.parse(json_strings);

            var dict:Dictionary = new Dictionary();

            for ( var i:String in config )
            {
                dict[i] = config[i];
            }

            return dict;
        }
    }

}
